package com.jspgou.cms.action.directive;

import static com.jspgou.cms.Constants.SHOP_SYS;
import static freemarker.template.ObjectWrapper.DEFAULT_WRAPPER;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;

import com.jspgou.cms.action.directive.abs.WebDirective;
import com.jspgou.cms.manager.ShipmentsMng;
import com.jspgou.common.page.Pagination;
import com.jspgou.common.web.freemarker.DirectiveUtils;
import com.jspgou.core.entity.Website;
import com.jspgou.core.manager.WebsiteMng;

import freemarker.core.Environment;
import freemarker.template.ObjectWrapper;
import freemarker.template.TemplateDirectiveBody;
import freemarker.template.TemplateException;
import freemarker.template.TemplateModel;

public class ShipmentPageDirective extends WebDirective{
	/**
	 * 模板名称
	 */
	public static final String TPL_NAME = "ArticlePage";

	public void execute(Environment evn, Map params, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		Website web=getWeb(evn, params, websiteMng);
		Integer count=getCount(params);
		Long memberId=getLong("memberId",params);
		Long storeId=getLong("storeId",params);
		Pagination pagination = shipmentMng.getFcList(memberId,storeId,getPageNo(evn),count);
		Map<String,TemplateModel> paramWarp=new HashMap<String,TemplateModel>(params);
		paramWarp.put(OUT_PAGINATION, ObjectWrapper.DEFAULT_WRAPPER.wrap(pagination));
		paramWarp.put(OUT_LIST, DEFAULT_WRAPPER.wrap(pagination.getList()));
		Map<String, TemplateModel> origMap = DirectiveUtils.addParamsToVariable(evn, paramWarp);
		if (isInvokeTpl(params)) {
	         includeTpl(SHOP_SYS, TPL_NAME, web, params, evn);
       } else {
       	renderBody(evn, loopVars, body);
       }
       DirectiveUtils.removeParamsFromVariable(evn, paramWarp, origMap);
	}
	
	private void renderBody(Environment evn, TemplateModel[] loopVars,
			TemplateDirectiveBody body) throws TemplateException, IOException {
		body.render(evn.getOut());
	}
	
	@Autowired
	private WebsiteMng websiteMng;
	@Autowired
	private ShipmentsMng shipmentMng;
}
